package service

import (
	"Graphite/database"
	"Graphite/model"
	"Graphite/util"
	"context"
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"go.mongodb.org/mongo-driver/bson"
	"log"
)

func FindRoleAPI(c *gin.Context, name model.Role)  {
	jsonStr:=util.Retrieve(c,"api","url","")
	var api  []model.Api
	_:json.Unmarshal(jsonStr,&api)

	jsonStr=util.Retrieve(c,"role","name",name.Name)
	var role  []model.Role
	_:json.Unmarshal(jsonStr,&role)

	var  NoApi  = api
	var  YesApi []model.Api
	for index,apiValue := range NoApi{
		for _,roleValue :=range role {
			for _,roleApi := range roleValue.Apis {
				if apiValue.Id == roleApi {
					YesApi = append(YesApi, NoApi[index] )
					NoApi = append(NoApi[:index], NoApi[index+1:]...)
				}
			}
		}
	}

	var data struct{YesApi []model.Api; NoApi []model.Api}
	data.YesApi = YesApi
	data.NoApi = NoApi
	c.JSON(200, gin.H{
		"code":1000,
		"data": data,
	})

	fmt.Println("YesApi:",YesApi)
	fmt.Println("NoApi:",NoApi)

}



func InsertRoleApi(c *gin.Context,role model.Role)  {
	var  one model.Role
	Collection := database.DB.Collection("role")
	err := Collection.FindOne(context.Background(), bson.M{"_id": role.Id}).Decode(&one)
	if err!=nil {
		util.Return(c,200,util.NOT_EXIST,err)
		return
	}
	one.Apis = append(one.Apis,role.Apis...)
	update := bson.M{"$set": one}
		updateResult, err := Collection.UpdateOne(context.Background(), bson.M{"_id":role.Id}, update)
		if err != nil {
			util.Return(c,200,util.EXCEPTION,err)
			log.Println(err)
		}
		util.Return(c,200,util.SUCCESS,updateResult)
		log.Println("collection.UpdateOne:", updateResult)
}



func DeleteRoleApi(c *gin.Context,role model.Role)  {
	var  one model.Role
	Collection := database.DB.Collection("role")
	err := Collection.FindOne(context.Background(), bson.M{"_id": role.Id}).Decode(&one)
	if err!=nil {
		util.Return(c,200,util.NOT_EXIST,err)
		return
	}
	One := one
	for i:=len(one.Apis)-1;i>=0;i-- {
		for j:=len(role.Apis)-1;j>=0;j-- {
			if one.Apis[i]==role.Apis[j] {
				One.Apis = append(One.Apis[:i],One.Apis[(i+1):]...)
			}
		}
	}
	fmt.Println(One)
	update := bson.M{"$set": One}
	updateResult, err := Collection.UpdateOne(context.Background(), bson.M{"_id":role.Id}, update)
	if err != nil {
		util.Return(c,200,util.EXCEPTION,err)
		log.Println(err)
	}
	util.Return(c,200,util.SUCCESS,updateResult)
	log.Println("collection.UpdateOne:", updateResult)
}



func RoleUpdate(c *gin.Context,role model.Role)  {
	var  one model.Role
	Collection := database.DB.Collection("role")
	err := Collection.FindOne(context.Background(), bson.M{"_id": role.Id}).Decode(&one)
	if err!=nil {
		util.Return(c,200,util.NOT_EXIST,err)
		return
	}
	one.Description = role.Description
	one.Name = role.Name
	update := bson.M{"$set": one}
	updateResult, err := Collection.UpdateOne(context.Background(), bson.M{"_id":role.Id}, update)
	if err != nil {
		util.Return(c,200,util.EXCEPTION,err)
		log.Println(err)
	}
	util.Return(c,200,util.SUCCESS,updateResult)
	log.Println("collection.UpdateOne:", updateResult)
}

